package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.exception.LeaseException;
import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentDetailVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentQueryVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentSubmitVo;
import com.atguigu.lease.web.admin.vo.fee.FeeValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【apartment_info(公寓信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo>
        implements ApartmentInfoService {
    @Autowired
    private GraphInfoService graphInfoService; //图片信息
    @Autowired
    private ApartmentFacilityService apartmentFacilityService;  //公寓配套
    @Autowired
    private ApartmentLabelService apartmentLabelService; //标签
    @Autowired
    private ApartmentFeeValueService apartmentFeeValueService; //杂费
    @Autowired
    private ProvinceInfoService provinceInfoService; //省份
    @Autowired
    private CityInfoService cityInfoService;//城市
    @Autowired
    private DistrictInfoService districtInfoService;//区县
    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;
    @Autowired
    private GraphInfoMapper graphInfoMapper;
    @Autowired
     private FacilityInfoMapper facilityInfoMapper;
    @Autowired
    private LabelInfoMapper labelInfoMapper;
    @Autowired
    private  FeeValueMapper feeValueMapper;
    @Autowired
    private RoomInfoService roomInfoService;
    @Override
    public void savelist(ApartmentSubmitVo apartmentSubmitVo) {
        boolean is = apartmentSubmitVo.getId() != null;

        //保存地区名
        //省份
        LambdaUpdateWrapper<ProvinceInfo> provinceInfoServiceLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        provinceInfoServiceLambdaUpdateWrapper.eq(ProvinceInfo::getId,apartmentSubmitVo.getProvinceId());
        ProvinceInfo provinceInfo = provinceInfoService.getOne(provinceInfoServiceLambdaUpdateWrapper);
        //城市
        LambdaUpdateWrapper<CityInfo> cityInfoLambdaUpdateWrapper= new LambdaUpdateWrapper<>();
        cityInfoLambdaUpdateWrapper.eq(CityInfo::getId,apartmentSubmitVo.getCityId());
        CityInfo cityInfo = cityInfoService.getOne(cityInfoLambdaUpdateWrapper);
        //区县
        LambdaUpdateWrapper<DistrictInfo> districtInfoLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        districtInfoLambdaUpdateWrapper.eq(DistrictInfo::getId,apartmentSubmitVo.getDistrictId());
        DistrictInfo districtInfo = districtInfoService.getOne(districtInfoLambdaUpdateWrapper);

        apartmentSubmitVo.setProvinceName(provinceInfo.getName());
        apartmentSubmitVo.setCityName(cityInfo.getName());
        apartmentSubmitVo.setDistrictName(districtInfo.getName());
        //首先保存传递的vo信息到数据库  myatisplus返回主键id 供后续取得主键值 保存公寓配套等其他信息
        saveOrUpdate(apartmentSubmitVo);

        if(is){
            //删除图片
            LambdaQueryWrapper<GraphInfo> graphInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
            graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemId,apartmentSubmitVo.getId());
            graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemType, ItemType.APARTMENT);
            graphInfoService.remove(graphInfoLambdaQueryWrapper);

            //删除配套
            LambdaQueryWrapper<ApartmentFacility> facilityLambdaQueryWrapper = new LambdaQueryWrapper<>();
            facilityLambdaQueryWrapper.eq(ApartmentFacility::getApartmentId,apartmentSubmitVo.getId());
            apartmentFacilityService.remove(facilityLambdaQueryWrapper);

            //删除标签
            LambdaQueryWrapper<ApartmentLabel> apartmentLabelLambdaQueryWrapper = new LambdaQueryWrapper<>();
            apartmentLabelLambdaQueryWrapper.eq(ApartmentLabel::getApartmentId,apartmentSubmitVo.getId());
            apartmentLabelService.remove(apartmentLabelLambdaQueryWrapper);

            //删除杂费
            LambdaQueryWrapper<ApartmentFeeValue> apartmentFeeValueLambdaQueryWrapper = new LambdaQueryWrapper<>();
            apartmentFeeValueLambdaQueryWrapper.eq(ApartmentFeeValue::getApartmentId,apartmentSubmitVo.getId());
            apartmentFeeValueService.remove(apartmentFeeValueLambdaQueryWrapper);
        }

        //保存图片
        List<GraphVo> graphVoList = apartmentSubmitVo.getGraphVoList();
        if(!CollectionUtils.isEmpty(graphVoList)){
            ArrayList<GraphInfo> graphInfos = new ArrayList<>(graphVoList.size());
            for (GraphVo graphVo : graphVoList) {
                GraphInfo graphInfo = new GraphInfo();
                graphInfo.setName(graphVo.getName());
                graphInfo.setUrl(graphVo.getUrl());
                graphInfo.setItemId(apartmentSubmitVo.getId());
                graphInfo.setItemType(ItemType.APARTMENT);
                graphInfos.add(graphInfo);
            }
            graphInfoService.saveBatch(graphInfos);
        }

        //保存配套
        List<Long> facilityInfoIds = apartmentSubmitVo.getFacilityInfoIds();
        if(!CollectionUtils.isEmpty(facilityInfoIds)){
            ArrayList<ApartmentFacility> facilities = new ArrayList<>(facilityInfoIds.size());
            for (Long facilityInfoId : facilityInfoIds) {
                ApartmentFacility facility = ApartmentFacility.builder().apartmentId(apartmentSubmitVo.getId()).facilityId(facilityInfoId).build();
                facilities.add(facility);
            }
            apartmentFacilityService.saveBatch(facilities);
        }

        //保存标签
        List<Long> labelIds = apartmentSubmitVo.getLabelIds();
        if(!CollectionUtils.isEmpty(labelIds)){
            ArrayList<ApartmentLabel> labelArrayList = new ArrayList<>(labelIds.size());
            for (Long labelId : labelIds) {
                ApartmentLabel label = ApartmentLabel.builder().apartmentId(apartmentSubmitVo.getId()).labelId(labelId).build();
                labelArrayList.add(label);
            }
            apartmentLabelService.saveBatch(labelArrayList);
        }

        //保存杂费
        List<Long> feeValueIds = apartmentSubmitVo.getFeeValueIds();
        if(!CollectionUtils.isEmpty(feeValueIds)){
            ArrayList<ApartmentFeeValue> feeValues = new ArrayList<>(feeValueIds.size());
            for (Long feeValueId : feeValueIds) {
                ApartmentFeeValue feeValue = ApartmentFeeValue.builder().apartmentId(apartmentSubmitVo.getId()).feeValueId(feeValueId).build();
                feeValues.add(feeValue);
            }
            apartmentFeeValueService.saveBatch(feeValues);
        }


    }

    @Override
    public void queryPage(Page<ApartmentItemVo> page, ApartmentQueryVo queryVo) {
         apartmentInfoMapper.queryPage(page,queryVo);
    }

    @Override
    public ApartmentDetailVo queryDetailById(Long id) {
        //查询公寓信息
        ApartmentInfo apartmentInfo = getById(id);
        //查询图片信息
        List<GraphVo> graphInfo = graphInfoMapper.queryGrapeInfo(id,ItemType.APARTMENT);
        //查询标签信息
        List<LabelInfo> labelInfo = labelInfoMapper.queryLableInfo(id);
        //查询配套信息
        List<FacilityInfo> facilityInfo = facilityInfoMapper.queryFacilityInfo(id);
        //查询杂费值
        List<FeeValueVo> feeValueVo =feeValueMapper.queryFeeInfo(id);
        ApartmentDetailVo apartmentDetailVo = new ApartmentDetailVo();

        apartmentDetailVo.setGraphVoList(graphInfo);
        apartmentDetailVo.setLabelInfoList(labelInfo);
        apartmentDetailVo.setFacilityInfoList(facilityInfo);
        apartmentDetailVo.setFeeValueVoList(feeValueVo);
        BeanUtils.copyProperties(apartmentInfo,apartmentDetailVo);
        return apartmentDetailVo;
    }

    @Override
    public void removeInfo(Long id) {
        LambdaQueryWrapper<RoomInfo> roomInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomInfoLambdaQueryWrapper.eq(RoomInfo::getApartmentId,id);
        long count = roomInfoService.count(roomInfoLambdaQueryWrapper);
        if(count>0){
            throw new LeaseException(ResultCodeEnum.DELETE_ERROR);
        }
        //删除公寓
        removeById(id);
        //删除图片
        LambdaQueryWrapper<GraphInfo> graphInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemType,ItemType.APARTMENT);
        graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemId,id);
        graphInfoService.remove(graphInfoLambdaQueryWrapper);
        //删除配套
        LambdaQueryWrapper<ApartmentFacility> facilityInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        facilityInfoLambdaQueryWrapper.eq(ApartmentFacility::getApartmentId,id);
        apartmentFacilityService.remove(facilityInfoLambdaQueryWrapper);
        //删除标签
        LambdaQueryWrapper<ApartmentLabel> apartmentLabelLambdaQueryWrapper = new LambdaQueryWrapper<>();
        apartmentLabelLambdaQueryWrapper.eq(ApartmentLabel::getApartmentId,id);
        //删除杂费
        LambdaQueryWrapper<ApartmentFeeValue> apartmentFeeValueLambdaQueryWrapper = new LambdaQueryWrapper<>();
        apartmentFeeValueLambdaQueryWrapper.eq(ApartmentFeeValue::getApartmentId,id);
        apartmentFeeValueService.remove(apartmentFeeValueLambdaQueryWrapper);

    }
}




